package com.yanti.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanti.domain.ResponseResult;
import com.yanti.domain.dto.UserAnswerSubmitDTO;
import com.yanti.domain.entity.UserAnswerDetail;
import com.yanti.mapper.UserAnswerDetailsMapper;
import com.yanti.service.UserAnswerDetailsService;
import com.yanti.service.UserDailyStudyTimeService;
import com.yanti.util.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * (UserAnswerDetails)表服务实现类
 *
 * @author luo
 * @since 2024-12-11 19:46:15
 */
@Service("userAnswerDetailsService")
public class UserAnswerDetailsServiceImpl extends ServiceImpl<UserAnswerDetailsMapper, UserAnswerDetail> implements UserAnswerDetailsService {

    @Resource
    private UserAnswerDetailsMapper userAnswerDetailMapper;

    @Resource
    private UserDailyStudyTimeService userDailyStudyTimeService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

//    @Override
//    public Integer countQuestionsByUid(String uid)
//    {
////        Date now = new Date();
////        QueryWrapper<UserAnswerDetail> queryWrapper = new QueryWrapper<>();
////        queryWrapper.eq("uid", uid)
////                .ge("created_at", now)
////                .lt("created_at", new Date(now.getTime() + 24 * 60 * 60 * 1000));
////        return Math.toIntExact(userAnswerDetailMapper.selectCount(queryWrapper));
//        // 获取当前时间
//        Date now = new Date();
//
//        // 获取今天零点的时间
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(now);
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//
//        Date startOfDay = calendar.getTime(); // 今天零点的时间
//
//        // 创建查询条件对象
//        QueryWrapper<UserAnswerDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("uid", uid)
//                // created_at 字段大于等于今天零点
//                .ge("created_at", startOfDay)
//                // created_at 字段小于当前时间
//                .lt("created_at", now);
//        // 执行查询并返回计数
//        return Math.toIntExact(userAnswerDetailMapper.selectCount(queryWrapper));
//    }

    @Override
    public ResponseResult countQuestionsByUid(String uid) {
        Integer questionCount = userDailyStudyTimeService.getUserQuestionCountInCache(uid);
        return ResponseResult.okResult(questionCount);
    }

//    @Override
//    public Integer countQuestionsByUid(String uid) {
//        Date now = new Date();
//        Date startOfDay = DateUtils.getStartOfDay(now);
//
//        // 先从数据库中查询当天正式提交的题目数量
//        QueryWrapper<UserAnswerDetail> queryWrapperDb = new QueryWrapper<>();
//        queryWrapperDb.eq("uid", uid).ge("created_at", startOfDay).lt("created_at", now).eq("is_temporary", 0);
//        int countFromDb = Math.toIntExact(userAnswerDetailMapper.selectCount(queryWrapperDb));
//
//        // 从 Redis 中获取当天暂存题目数量（之前在暂存时记录的当天暂存题目数）
//        String todayTemporaryCountKey = "today_temporary_count:" + uid;
//        Object countObj = redisTemplate.opsForValue().get(todayTemporaryCountKey);
//        Long todayTemporaryCount = 0L;
//        if (countObj instanceof Number) {
//            todayTemporaryCount = ((Number) countObj).longValue();
//        }
//        return countFromDb + todayTemporaryCount.intValue();
//    }

    @Override
    public boolean isAnswerTemporary(String userId, Long questionId) {
        QueryWrapper<UserAnswerDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId).eq("question_id", questionId).eq("is_temporary", 1);
        return getOne(queryWrapper)!= null;
    }

}